Completed
Branch master (ebb499)
by Alexey
04:15
created

Modals.show   B

Complexity

Conditions 5
Paths 10

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 10
nop 4
dl 0
loc 38
rs 8.439
c 0
b 0
f 0
1
/**
2
 * Main Ui object
3
 * 
4
 * @returns {Ui}
5
 */
6
inji.Ui = new function () {
7
  inji.onLoad(function () {
8
    inji.Ui.bindMenu($('.nav-list-categorys'));
9
    inji.Ui.modals = new Modals();
10
    inji.Ui.forms = new Forms();
11
    inji.Ui.editors = new Editors();
12
  });
13
14
  this.bindMenu = function (container) {
15
    container.find('.nav-left-ml').toggle();
16
    container.find('label.nav-toggle span').click(function () {
17
      $(this).parent().parent().children('ul.nav-left-ml').toggle(300);
18
      var cs = $(this).attr("class");
19
      if (cs == 'nav-toggle-icon glyphicon glyphicon-chevron-right') {
20
        $(this).removeClass('glyphicon-chevron-right').addClass('glyphicon-chevron-down');
21
      }
22
      if (cs == 'nav-toggle-icon glyphicon glyphicon-chevron-down') {
23
        $(this).removeClass('glyphicon-chevron-down').addClass('glyphicon-chevron-right');
24
      }
25
    });
26
  };
27
  this.requestInfo = function (options, callback) {
28
    var id = 'resultForm' + inji.randomString();
29
    var body = '<form id ="' + id + '">';
30
    body += '<h2>' + options.header + '</h2>';
31
    for (var key in options.inputs) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
32
      body += '<div class = "form-group">';
33
      body += '<label>' + options.inputs[key].label + '</label>';
34
      body += '<input type = "' + options.inputs[key].type + '" name = "' + key + '" class ="form-control" />';
35
      body += '</div>';
36
    }
37
    body += '<button class = "btn btn-primary">' + options.btn + '</button>';
38
    body += '</form>';
39
    var modal = inji.Ui.modals.show('', body);
40
    $('#' + id).on('submit', function () {
41
      callback($('#' + id).serializeArray());
42
      modal.modal('hide');
43
      return false;
44
    });
45
  }
46
}
47
48
49
50
/**
51
 * Editors
52
 * 
53
 */
54
Editors = function () {
0 ignored issues
show
Bug introduced by
The variable Editors seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.Editors.
Loading history...
55
  this.ckeditor = false;
56
  this.checkEditors();
57
  inji.on('loadScript', function () {
58
    inji.Ui.editors.checkEditors();
59
  });
60
  inji.onLoad(function () {
61
    inji.Ui.editors.loadIn('.htmleditor');
62
  })
63
}
64
Editors.prototype.checkEditors = function () {
65
  if (!this.ckeditor && typeof CKEDITOR != 'undefined') {
66
    this.ckeditor = true;
67
  }
68
}
69
Editors.prototype.loadAll = function () {
70
71
}
72
Editors.prototype.loadIn = function (selector, search) {
73
  if (this.ckeditor) {
74
    setTimeout(function () {
75
      var instances;
76
      if (typeof search != 'undefined') {
77
        instances = $(selector).find(search);
78
      } else {
79
        instances = $(selector);
80
      }
81
      $.each(instances, function () {
82
        var editor;
83
        var _this = this;
84
        if ($(this).closest('.modal').length == 0 || $(this).closest('.modal').hasClass('in')) {
85
          editor = $(_this).ckeditor({customConfig: inji.options.appRoot + 'static/moduleAsset/libs/libs/ckeditor/program/userConfig.php'});
86
        }
87
        if ($(this).closest('.modal').length != 0) {
88
          $(this).closest('.modal').on('shown.bs.modal', function () {
89
            setTimeout(function () {
90
              editor = $(_this).ckeditor({customConfig: inji.options.appRoot + 'static/moduleAsset/libs/libs/ckeditor/program/userConfig.php'});
91
            }, 1000);
92
          })
93
          $(this).closest('.modal').on('hide.bs.modal', function () {
94
            if (editor.editor) {
0 ignored issues
show
Bug introduced by
The variable editor does not seem to be initialized in case $(this).closest(".modal"....modal").hasClass("in") on line 84 is false. Are you sure this can never be the case?
Loading history...
95
              editor.editor.updateElement();
96
              editor.editor.destroy();
97
              delete editor.editor
98
              $(this).closest('.modal').unbind('hide.bs.modal');
99
              $(this).closest('.modal').unbind('shown.bs.modal');
100
            }
101
102
          })
103
        }
104
      })
105
    }, 1000);
106
  }
107
}
108
Editors.prototype.beforeSubmit = function (form) {
109
  if (this.ckeditor) {
110
    $.each(CKEDITOR.instances, function () {
111
      this.updateElement();
112
    })
113
    $.each($(form).find('.cke'), function () {
114
      var instance = $(this).attr('id').replace('cke_', '');
115
      $(CKEDITOR.instances[instance].element).closest('.modal').unbind();
116
      CKEDITOR.instances[instance].destroy();
117
    });
118
  }
119
}
120
/**
121
 * Modals objects
122
 * 
123
 * @returns {Modals}
124
 */
125
Modals = function () {
0 ignored issues
show
Bug introduced by
The variable Modals seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.Modals.
Loading history...
126
  this.modals = 0;
127
}
128
Modals.prototype.show = function (title, body, code, size) {
129
  if (code == null) {
130
    code = 'modal' + (++this.modals);
131
  }
132
  if ($('#' + code).length == 0) {
133
    if (size == null) {
134
      size = '';
135
    }
136
    if (title) {
137
      title = '<div class="modal-header">\
138
                  <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\
139
                  <h4 class="modal-title">' + title + '</h4>\
140
                </div>';
141
    } else {
142
      title = '';
143
    }
144
    var html = '\
145
          <div class="modal fade" id = "' + code + '" >\
146
            <div class="modal-dialog ' + size + '">\
147
              <div class="modal-content">\
148
                ' + title + '\
149
                <div class="modal-body">\
150
                ' + body + '\
151
                </div>\
152
                <div class="modal-footer">\
153
                  <button type="button" class="btn btn-default" data-dismiss="modal">Закрыть</button>\
154
                </div>\
155
              </div>\
156
            </div>\
157
          </div>';
158
    $('body').append(html);
159
160
  }
161
  var modal = $('#' + code);
162
  $('body').append(modal);
163
  modal.modal('show');
164
  return modal;
165
}
166
/**
167
 * Forms object
168
 * 
169
 * @returns {Forms}
170
 */
171
function Forms() {
172
  this.dataManagers = 0;
173
  this.formCallbacks = {};
174
}
175
Forms.prototype.popUp = function (item, params, callback) {
176
  var code = item;
177
178
  if (typeof params == 'undefined') {
179
    params = {};
180
  }
181
  if (typeof (params.relation) != 'undefined') {
182
    code += params.relation;
183
  }
184
  code = code.replace(/:/g, '_').replace(/\\/g, '_');
185
  var exist = false;
0 ignored issues
show
Unused Code introduced by
The variable exist seems to be never used. Consider removing it.
Loading history...
186
  if ($('#' + code).length != 0) {
187
    exist = true;
188
  }
189
  var modal = inji.Ui.modals.show('', '<div class = "text-center"><img src = "' + inji.options.appRoot + 'static/moduleAsset/Ui/images/ajax-loader.gif" /></div>', code, 'modal-lg');
190
  //if (!exist) {
191
  inji.Server.request({
192
    url: 'ui/formPopUp/',
193
    data: {item: item, params: params},
194
    success: function (data) {
195
      modal.find('.modal-body').html(data);
196
      if (callback) {
197
        inji.Ui.forms.formCallbacks[modal.find('.form').attr('id')] = callback;
198
      }
199
      inji.Ui.editors.loadIn(modal.find('.modal-body'), '.htmleditor');
200
    }
201
  });
202
  //}
203
}
204
Forms.prototype.submitAjax = function (form, params) {
205
  inji.Ui.editors.beforeSubmit(form);
206
  var form = $(form);
207
  var container = form.parent().parent();
208
  var btn = form.find('button');
209
  btn.text('Подождите');
210
  btn[0].disabled = true;
211
  btn.data('loading-text', "Подождите");
212
213
  var url = form.attr('action');
214
  if (params) {
215
    var first = true;
216
    if (url.indexOf('?') >= 0) {
217
      first = false;
218
    }
219
    for (var key in params) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
220
      url += (first ? '?' : '&') + key + '=' + params[key];
221
    }
222
  }
223
  var formData = new FormData(form[0]);
224
  inji.Server.request({
225
    url: url,
226
    type: 'POST',
227
    data: formData,
228
    processData: false,
229
    success: function (data) {
230
      if (inji.Ui.forms.formCallbacks[form.attr('id')]) {
231
        inji.Ui.forms.formCallbacks[form.attr('id')]();
232
        delete inji.Ui.forms.formCallbacks[form.attr('id')];
233
      }
234
      container.html(data);
235
      inji.Ui.editors.loadIn(container, '.htmleditor');
236
      inji.Ui.dataManagers.reloadAll();
237
      if (params && !params.notSave) {
238
        var btn = container.find('form button');
239
        var text = btn.text();
240
        btn.text('Изменения сохранены!');
241
        setTimeout(function () {
242
          btn.text(text)
243
        }, 3000);
244
      }
245
    }
246
  });
247
}
248
Forms.prototype.addRowToList = function (btn) {
249
  var container = $(btn).closest('.dynamicList');
250
  var counter = parseInt(container.find('.sourceRow').data('counter')) + 1;
251
  container.find('.sourceRow').data('counter', counter);
252
  var trHtml = container.find('.sourceRow script').html().replace(/^\/\*/g, '').replace(/\*\/$/g, '').replace(/\[counterPlaceholder\]/g, '[' + counter + ']');
253
  container.find('.listBody').append(trHtml);
254
}
255
Forms.prototype.checkAditionals = function (select) {
256
  var selectedInputAd = $(select).find('option:selected').attr('data-aditionalInput');
257
  var nextSelect = $(select).next();
258
  i = 0;
0 ignored issues
show
Bug introduced by
The variable i seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.i.
Loading history...
259
  if ($(select).data('aditionalEnabled') == 1) {
260
    $(select).data('aditionalEnabled', 0);
261
  }
262
  while (nextSelect.length) {
263
    if (i != selectedInputAd) {
264
      nextSelect[0].disabled = true;
265
      nextSelect.addClass('hidden');
266
    } else {
267
      if ($(select).data('aditionalEnabled') != 1) {
268
        $(select).data('aditionalEnabled', 1);
269
      }
270
      nextSelect[0].disabled = false;
271
      nextSelect.removeClass('hidden');
272
    }
273
    nextSelect = $(nextSelect).next();
274
    i++;
275
  }
276
}
277
Forms.prototype.delRowFromList = function (btn) {
278
  $(btn).closest('tr').remove();
279
};
280
281
inji.Ui.activeForms = new function () {
282
  this.activeForms = [];
283
  this.get = function (selector) {
284
    var element = inji.get(selector);
285
    if (element && element.data('activeFormIndex') !== null) {
286
      return this.activeForms[element.data('activeFormIndex')];
287
    }
288
    this.initial(element);
289
  };
290
  this.initial = function (element) {
291
    var activeForm = new ActiveForm();
292
    this.activeForms.push(activeForm);
293
294
    activeForm.index = this.activeForms.length - 1;
295
    activeForm.element = element;
296
    activeForm.modelName = element.data('modelname');
297
    activeForm.formName = element.data('formname');
298
    activeForm.inputs = element.data('inputs');
299
300
    element.element.setAttribute('activeFormIndex', activeForm.index);
301
302
    activeForm.load();
303
  }
304
}
305
function ActiveForm() {
306
  this.modelName;
0 ignored issues
show
introduced by
The result of the property access to this.modelName is not used.
Loading history...
307
  this.formName;
0 ignored issues
show
introduced by
The result of the property access to this.formName is not used.
Loading history...
308
  this.reqestProcess;
0 ignored issues
show
introduced by
The result of the property access to this.reqestProcess is not used.
Loading history...
309
  this.inputs = {};
310
  this.index;
0 ignored issues
show
introduced by
The result of the property access to this.index is not used.
Loading history...
311
  this.element;
0 ignored issues
show
introduced by
The result of the property access to this.element is not used.
Loading history...
312
  this.load = function () {
313
    for (var inputName in this.inputs) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
314
      var inputParams = this.inputs[inputName];
315
      var self = this;
316
      if (this.inputHandlers[inputParams.type]) {
317
        var query = '#' + this.element.element.id + ' [name="query-ActiveForm_' + this.formName + '[' + this.modelName.replace(/\\/g, '\\\\') + '][' + inputName + ']"]';
318
        this.inputHandlers[inputParams.type](inji.get(query), inputName, this);
319
      }
320
      if (inputParams.onChange == 'reloadForm') {
321
        var query = '#' + this.element.element.id + ' [name="ActiveForm_' + this.formName + '[' + this.modelName.replace(/\\/g, '\\\\') + '][' + inputName + ']"]';
322
        $(query).on('change', function () {
323
          inji.Ui.forms.submitAjax($('#' + self.element.element.id + ' form')[0], {notSave: true});
0 ignored issues
show
Bug introduced by
The variable self is changed as part of the for-each loop for example by this on line 315. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
324
        })
325
      }
326
    }
327
  };
328
  this.inputHandlers = {
329
    search: function (element, inputName, activeForm) {
330
      element.element.onkeyup = function () {
331
        var inputContainer = element.element.parentNode;
332
        var selectedDiv = inputContainer.querySelector('.form-search-cur');
333
        var resultsDiv = inputContainer.querySelector('.form-search-results');
334
        resultsDiv.innerHTML = '<div class = "text-center"><img src = "' + inji.options.appRoot + 'static/moduleAsset/Ui/images/ajax-loader.gif" /></div>';
335
        if (this.reqestProcess) {
336
          this.reqestProcess.abort()
337
        }
338
        this.reqestProcess = inji.Server.request({
339
          url: 'ui/activeForm/search',
340
          data: {
341
            modelName: activeForm.modelName,
342
            formName: activeForm.formName,
343
            inputName: inputName,
344
            search: this.value
345
          },
346
          success: function (results) {
347
            resultsDiv.innerHTML = '';
348
            for (var key in results) {
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
349
              var result = results[key];
350
              var resultElement = document.createElement("div");
351
              resultElement.setAttribute('objectid', key);
352
              resultElement.appendChild(document.createTextNode(result));
353
              resultElement.onclick = function () {
354
                var value = 0;
355
                for (key in this.attributes) {
356
                  if (this.attributes[key].name == 'objectid') {
0 ignored issues
show
introduced by
The variable key is changed by the for-each loop on line 355. Only the value of the last iteration will be visible in this function if it is called outside of the loop.
Loading history...
357
                    value = this.attributes[key].value;
358
                  }
359
                }
360
                inputContainer.querySelector('[type="hidden"]').value = value;
361
                inputContainer.querySelector('[type="text"]').value = this.innerHTML;
362
                selectedDiv.innerHTML = 'Выбрано: ' + this.innerHTML;
363
                resultsDiv.innerHTML = '';
364
              }
365
              resultsDiv.appendChild(resultElement);
366
            }
367
            resultsDiv.style.display = 'block';
368
          }
369
        })
370
      };
371
    }
372
  };
373
}